package com.angenela.imageshare.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.angenela.imageshare.R;
import com.angenela.imageshare.pojo.CollectImage;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class myCollect extends AppCompatActivity {

    private static final String HOME_URL = "http://122.9.152.182:8080/star/";
    public static String ImageId = "imageId";
    public static String ImageSrc = "imageSrc";
    public static String ImageStar = "imageStar";
    public static String ImageUserId = "imageUserId";
    public static String ImageUsername = "imageUsername";
    public static String ImageAvatar = "imageAvatar";
    public static String ImageFlag = "imageFlag";
    public static Integer IMAGE_INFO_REQUEST_CODE = 100;
    GridView gridView;
    CollectImage userAndImageList;
    List<Integer> userStarInfo = new ArrayList<>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == IMAGE_INFO_REQUEST_CODE.intValue())
            if (resultCode == 1) {
                List<CollectImage.CollectInfo> images = userAndImageList.getData();
                int i;
                for (i = 0; i < images.size(); i++) {
                    if (images.get(i).getImageId().equals(resultCode))
                        gridView.getChildAt(i).setVisibility(View.INVISIBLE);
                }
            }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        Intent intent = new Intent(this, ImageActivity.class);
        gridView = findViewById(R.id.collect_image);
        try {
            userAndImageList = getImageCollect();
            CollectAdapter collectAdapter = new CollectAdapter(this, userAndImageList.getData());
            gridView.setAdapter(collectAdapter);
        } catch (IOException e) {
            e.printStackTrace();
        }

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent.putExtra(ImageSrc, userAndImageList.getData().get(position).getSrc());
                intent.putExtra(ImageId, userAndImageList.getData().get(position).getImageId());
                intent.putExtra(ImageStar, userAndImageList.getData().get(position).getStar());
                intent.putExtra(ImageUserId, userAndImageList.getData().get(position).getUserId());
                intent.putExtra(ImageUsername, userAndImageList.getData().get(position).getUsername());
                intent.putExtra(ImageAvatar, userAndImageList.getData().get(position).getAvatar());
                intent.putExtra(ImageFlag, 1);
//                startActivity(intent);
                startActivityForResult(intent, IMAGE_INFO_REQUEST_CODE);
            }
        });
    }

    public CollectImage getImageCollect() throws IOException {

        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder()
                .url(HOME_URL + "info/" + getUserId())
                .get()
                .build();

        Response response = okHttpClient.newCall(request).execute();
        Gson gson = new Gson();
        Type jsonType = new TypeToken<CollectImage>() {
        }.getType();
        CollectImage result = gson.fromJson(response.body().string(), jsonType);

        return result;

    }


    public Integer getUserId() {
        Resources resources = getResources();
        String spFileNameKey = resources.getString(R.string.shared_preferences_file_name);
        String idKey = resources.getString(R.string.login_account_id);
        SharedPreferences sharedPreferences =
                this.getSharedPreferences(spFileNameKey, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(idKey, 0);
    }


    public class CollectAdapter extends BaseAdapter {


        int count = 0;
        private Context mContext;
        private LayoutInflater layoutInflater;
        private List<CollectImage.CollectInfo> userAndImageList;

        public CollectAdapter(Context context, List<CollectImage.CollectInfo> userAndImageList) {
            this.mContext = context;
            this.userAndImageList = userAndImageList;
            count = userAndImageList.size();
            layoutInflater = LayoutInflater.from(context);
        }


        @Override
        public int getCount() {
            return count;
        }

        @Override
        public Object getItem(int position) {
            return userAndImageList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CollectImage.CollectInfo userImageId = userAndImageList.get(position);
            ViewHolder holder;

            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.myimage, null);
                holder = new ViewHolder();
                holder.imageView = convertView.findViewById(R.id.iv_MyImage);
                holder.tv_imageId = convertView.findViewById(R.id.tv_ImageId);

                convertView.setTag(holder);
            } else {
                holder = (CollectAdapter.ViewHolder) convertView.getTag();

            }

            holder.tv_imageId.setText(String.valueOf(userImageId.getImageId()));
            Glide.with(mContext).load(userImageId.getSrc()).into(holder.imageView);


            return convertView;
        }

        public class ViewHolder {
            public ImageView imageView;
            public TextView tv_imageId;
        }
    }
}
